import requests
import app


class IhrmLogin:

    def login(self, data):
        response = requests.post(url=app.BASE_URL + "/api/sys/login",
                                 headers=app.HEADERS,
                                 json=data)
        print(response.url)
        print(response.json())

        # 2 提取令牌
        token = response.json().get("data")
        app.HEADERS["Authorization"] = "Bearer " + token
        print("保存到全局变量HEADERS为：", app.HEADERS)
        return response

    def select_list(self):  # 3 查询员工列表
        response = requests.get(url=app.BASE_URL + "/api/sys/user",
                                params="page=1&size=10",
                                headers=app.HEADERS)

        print(response.json())  # 打印查询员工的结果
        return response

    def add_emp(self, data):  # 4 添加员工
        response = requests.post(url=app.BASE_URL + "/api/sys/user",
                                 headers=app.HEADERS,
                                 json=data)
        print(response.json())

        emp_id = response.json().get('data').get('id')  # 获取员工ID
        app.EMP_ID = emp_id  # 设置员工ID为全局变量
        print(emp_id)
        return response

    def select_emp(self):  # 查询员工
        response = requests.get(url=app.BASE_URL + "/api/sys/user" + "/" + app.EMP_ID,
                                headers=app.HEADERS)
        print(response.json())
        return response

    def update_emp(self, data):  # 修改员工
        response = requests.put(url=app.BASE_URL + "/api/sys/user" + "/" + app.EMP_ID,
                                headers=app.HEADERS,
                                json=data)
        print(response.json())
        return response

    def delete_emp(self):  # 删除员工
        response = requests.delete(url=app.BASE_URL + "/api/sys/user" + "/" + app.EMP_ID,
                                   headers=app.HEADERS)
        print(response.json())
        return response


if __name__ == '__main__':
    man = IhrmLogin()
    man.login({'mobile': '13800000002', 'password': '123456'})
    man.select_list()
    man.add_emp({"username": "杀手格拉斯",
                 "mobile": "18343216230",
                 "workNumber": "001",
                 "timeOfEntry": "2020-01-01",
                 "forOfEmployement": "1",
                 "departmentId": "9993",
                 "departmentName": "深圳18期测试部",
                 "correctionTime": "2020-01-01"})
    man.update_emp({"username": "美少女战士"})
    man.delete_emp()
